package cn.kow.kmall.service.impl;

import cn.kow.kmall.core.constant.KmallStatusConstant;
import cn.kow.kmall.dao.OrderAfterSaleRepository;
import cn.kow.kmall.pojo.dto.OrderAfterSaleQueryDTO;
import cn.kow.kmall.pojo.entity.OrderAfterSale;
import cn.kow.kmall.service.OrderAfterSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Random;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2020-05-06 19:48
 * @Modified By:
 */
@Service("orderAfterSaleService")
public class OrderAfterSaleServiceImpl implements OrderAfterSaleService {

    @Autowired
    private OrderAfterSaleRepository orderAfterSaleRepository;

    @Override
    public OrderAfterSale create(OrderAfterSale afterSale) {
        String serviceNo = createOrderRefundNo();
        afterSale.setServiceNo(serviceNo);
        afterSale.setCreateTime(new Date());
        return orderAfterSaleRepository.create(afterSale);
    }

    @Override
    public void update(OrderAfterSale afterSale) {
        orderAfterSaleRepository.update(afterSale);
    }

    /**
     * 生成订单号
     * @return 订单号
     */
    private String createOrderRefundNo() {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        Random rand = new Random();
        int j = rand.nextInt(10);
        int k = rand.nextInt(10);
        String orderNo = 8 + dateTime.format(formatter) + j + k;
        return orderNo;
    }

    @Override
    public Page<OrderAfterSale> listByPage(OrderAfterSaleQueryDTO queryDTO) {
        Page<OrderAfterSale> page = orderAfterSaleRepository.queryByPage(queryDTO);
        return page;
    }

    @Override
    public OrderAfterSale getByServiceNo(String serviceNo) {
        OrderAfterSale record = new OrderAfterSale();
        record.setServiceNo(serviceNo);
        record.setStatus(KmallStatusConstant.NORMAL);
        return orderAfterSaleRepository.getOne(record);
    }

}
